<?php



/**

 * ECSHOP mobile前台公共函数

 * ============================================================================

 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。

 * 网站地址: http://www.ecshop.com；

 * ----------------------------------------------------------------------------

 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和

 * 使用；不允许对程序代码以任何形式任何目的的再发布。

 * ============================================================================

 * $Author: testyang $

 * $Id: lib_main.php 15013 2008-10-23 09:31:42Z testyang $

*/



if (!defined('IN_ECS'))

{

	die('Hacking attempt');

}



/**

 * 获得分类下的商品

 *

 * @access  public

 * @param   string  $children

 * @return  array

 */

function get_cat_goods($cat = '', $size=0, $brand = 0, $min = 0, $max = 0, $ext = '', $sort='', $order='')

{

	$children=get_children($cat);

    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".

            "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';



    if ($brand > 0)

    {

        $where .=  "AND g.brand_id=$brand ";

    }



    if ($min > 0)

    {

        $where .= " AND g.shop_price >= $min ";

    }



    if ($max > 0)

    {

        $where .= " AND g.shop_price <= $max ";

    }



    /* 获得商品列表 */

    $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, ' .

                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, " .

                'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img ' .

            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .

            'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . ' AS mp ' .

                "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .

            "WHERE $where $ext ORDER BY $sort $order g.sort_order, g.last_update DESC";

			

    $res = $GLOBALS['db']->selectLimit($sql, $size);





    $arr = array();

    while ($row = $GLOBALS['db']->fetchRow($res))

    {

        if ($row['promote_price'] > 0)

        {

            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);

        }

        else

        {

            $promote_price = 0;

        }



        /* 处理商品水印图片 */

        $watermark_img = '';



        if ($promote_price != 0)

        {

            $watermark_img = "watermark_promote_small";

        }

        elseif ($row['is_new'] != 0)

        {

            $watermark_img = "watermark_new_small";

        }

        elseif ($row['is_best'] != 0)

        {

            $watermark_img = "watermark_best_small";

        }

        elseif ($row['is_hot'] != 0)

        {

            $watermark_img = 'watermark_hot_small';

        }



        if ($watermark_img != '')

        {

            $arr[$row['goods_id']]['watermark_img'] =  $watermark_img;

        }



        $arr[$row['goods_id']]['id']         = $row['goods_id'];

        $arr[$row['goods_id']]['name']       = $row['goods_name'];

        $arr[$row['goods_id']]['brief']      = $row['goods_brief'];

        $arr[$row['goods_id']]['style_name'] = add_style($row['goods_name'],$row['goods_name_style']);

        $arr[$row['goods_id']]['market_price']     = price_format($row['market_price']);

        $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);

        $arr[$row['goods_id']]['type']             = $row['goods_type'];

        $arr[$row['goods_id']]['promote_price']    = ($promote_price > 0) ? price_format($promote_price) : '';

        $arr[$row['goods_id']]['thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);

        $arr[$row['goods_id']]['img']        = get_image_path($row['goods_id'], $row['goods_img']);

        $arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);

		$arr[$row['goods_id']]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];

		$arr[$row['goods_id']]['short_style_name'] = add_style($arr[$row['goods_id']]['short_name'],$row['goods_name_style']);



    }



    return $arr;

}



/**

 * 获得促销商品

 *

 * @access  public

 * @return  array

 */

function get_promote_goods_wap($cats = '')

{

    $time = gmtime();

    $order_type = $GLOBALS['_CFG']['recommend_order'];



    /* 取得促销lbi的数量限制 */

    $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .

                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".

                "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, b.brand_name, " .

                "g.is_best, g.is_new, g.is_hot, g.is_promote, RAND() AS rnd " .

            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .

            'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .

            "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".

                "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".

            'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' .

            " AND g.is_promote = 1 AND promote_start_date <= '$time' AND promote_end_date >= '$time' ";

    $sql .= $order_type == 0 ? ' ORDER BY g.sort_order, g.last_update DESC' : ' ORDER BY rnd';

    $result = $GLOBALS['db']->getAll($sql);



    $goods = array();

    foreach ($result AS $idx => $row)

    {

        if ($row['promote_price'] > 0)

        {

        $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);

        $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';

        $goods[$idx]['zhekou_price']   = round(($promote_price/$row['market_price'])*100)+"";

         $goods[$idx]['sheng_price'] = price_format($row['market_price']-$promote_price);

       }

        else

        {

        $goods[$idx]['promote_price'] = '';

        $goods[$idx]['zhekou_price']   = round(($row['shop_price']/$row['market_price'])*100)+"";

        $goods[$idx]['sheng_price'] = price_format($row['market_price']-$row['shop_price']);

       }



        $goods[$idx]['id']           = $row['goods_id'];

        $goods[$idx]['name']         = $row['goods_name'];

        $goods[$idx]['brief']        = $row['goods_brief'];

        $goods[$idx]['brand_name']   = $row['brand_name'];

        $goods[$idx]['goods_style_name']   = add_style($row['goods_name'],$row['goods_name_style']);

        $goods[$idx]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];

        $goods[$idx]['short_style_name']   = add_style($goods[$idx]['short_name'],$row['goods_name_style']);

        $goods[$idx]['market_price'] = price_format($row['market_price']);

        $goods[$idx]['shop_price']   = price_format($row['shop_price']);

        $goods[$idx]['thumb']        = get_image_path($row['goods_id'], $row['goods_thumb'], true);

        $goods[$idx]['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);

        $goods[$idx]['url']          = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);

   }



    return $goods;

}



/**

 * wap根据广告位名称调用广告

 *

 * position_name        广告位名称

 * type                 媒介类型

 * num                  调用数量

 */

function get_advlist_position_name($position_name,$type,$num)

{

		$arr = array( );

		$sql = "select ap.ad_width,ap.ad_height,ad.ad_id,ad.ad_name,ad.ad_code,ad.media_type,ad.ad_link,ad.ad_id from ".$GLOBALS['ecs']->table( "ad_position" )." as ap left join ".$GLOBALS['ecs']->table( "ad" )." as ad on ad.position_id = ap.position_id where ap.position_name='".$position_name.( "' and UNIX_TIMESTAMP()>ad.start_time and UNIX_TIMESTAMP()<ad.end_time and ad.media_type=".$type." and ad.enabled=1 limit ".$num );

		$res = $GLOBALS['db']->getAll( $sql );

		foreach ( $res as $idx => $row )

		{
				
				$temp = array();
			
				$temp['label'] = $row['ad_name'];

// 				$temp['code'] = $row['ad_code'];

// 				$temp['url'] = "affiche.php?ad_id=".$row['ad_id']."&uri=".$row['ad_link'];

				$temp['img'] = "../data/afficheimg/".$row['ad_code'];

// 				$temp['content'] = "<a href='".$temp['url']."' target='_blank'><img src='data/afficheimg/".$row['ad_code']."' width='".$row['ad_width']."' height='".$row['ad_height']."' /></a>";

// 				$temp['width'] = $row['ad_width'];

// 				$temp['height'] = $row['ad_height'];
				
				$arr[] = $temp;

		}

		return $arr;

}



/**

 * 对输出编码

 *

 * @access  public

 * @param   string   $str

 * @return  string

 */

function encode_output($str)

{

	return htmlspecialchars($str);

}



/**

 * wap分页函数

 *

 * @access		public

 * @param		int		$num		总记录数

 * @param		int		$perpage	每页记录数

 * @param		int		$curr_page	当前页数

 * @param		string	$mpurl		传入的连接地址

 * @param		string	$pvar		分页变量

 */

function get_wap_pager($num, $perpage, $curr_page, $mpurl,$pvar)

{

	$multipage = '';

	if($num > $perpage)

	{

		$page = 10;

		$offset = 1;

		$pages = ceil($num / $perpage);

		$all_pages = $pages;

		$tmp_page = $curr_page;

		$setp = strpos($mpurl, '?') === false ? "?" : '&amp;';

		$multipage .= "<div id=\"pager\" class=\"page clearfix\">";

		if($curr_page > 1)

		{

			$multipage .= "<a href=\"$mpurl${setp}${pvar}=".($curr_page-1)."\" class=\"perv_page\"><span></span>上一页</a> ";

		}else{

			$multipage .= "<a class=\"perv_page_no\">上一页</a> ";

		}

		$multipage .= "<span class=\"cur_page\">".$curr_page."/".$pages."</span>";

		if(($curr_page++) < $pages)

		{

			$multipage .= " <a href=\"$mpurl${setp}${pvar}=".$curr_page++."\" class=\"next_page\"><span></span>下一页</a><br/>";

		}else{

			$multipage .= "<a class=\"next_page_no\">下一页</a> ";

		}

		$multipage .= "</div>";

	}

	return $multipage;

}



/**

 * 返回尾文件

 *

 * @return	string

 */

function get_footer()

{

	if ($_SESSION['user_id'] > 0)

	{

		$footer = "<br/><a href='user.php?act=user_center'>用户中心</a>|<a href='user.php?act=logout'>退出</a>|<a href='javascript:scroll(0,0)' hidefocus='true'>回到顶部</a><br/>Copyright 2009<br/>Powered by ECShop v2.7.2";

	}

	else

	{

		$footer = "<br/><a href='user.php?act=login'>登陆</a>|<a href='user.php?act=register'>免费注册</a>|<a href='javascript:scroll(0,0)' hidefocus='true'>回到顶部</a><br/>Copyright 2009<br/>Powered by ECShop v2.7.2";

	}



	return $footer;

}



/**

 * 获得商品的详细信息

 *

 * @access  public

 * @param   integer     $goods_id

 * @return  void

 */

function get_goods_info2($goods_id)

{

    $time = gmtime();

    $sql = 'SELECT g.*, c.measure_unit, b.brand_id, b.brand_name AS goods_brand, m.type_money AS bonus_money, ' .

                'IFNULL(AVG(r.comment_rank), 0) AS comment_rank, ' .

                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS rank_price " .

			',(select count(r.id_value) from '.$GLOBALS['ecs']->table( "comment" ).' as r where r.id_value = g.goods_id and r.comment_type = 0 AND r.parent_id = 0 AND r.status = 1) AS comment_count,(select IFNULL(sum(og.goods_number),0) from '.$GLOBALS['ecs']->table( "order_goods" ).' as og where og.goods_id = g.goods_id) AS sell_number '.

			',(select count(r.id_value) from '.$GLOBALS['ecs']->table( "comment" ).' as r where r.id_value = g.goods_id AND r.comment_type = 0 AND r.parent_id = 0 AND r.status = 1 and comment_rank>3) AS comment_count5,(select count(r.id_value) from '.$GLOBALS['ecs']->table( "comment" ).' as r where r.id_value = g.goods_id AND r.comment_type = 0 AND r.parent_id = 0 AND r.status = 1 and comment_rank=3) AS comment_count3,(select count(r.id_value) from '.$GLOBALS['ecs']->table( "comment" ).' as r where r.id_value = g.goods_id AND r.comment_type = 0 AND r.parent_id = 0 AND r.status = 1 and comment_rank<3) AS comment_count1 '.

            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .

            'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS c ON g.cat_id = c.cat_id ' .

            'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON g.brand_id = b.brand_id ' .

            'LEFT JOIN ' . $GLOBALS['ecs']->table('comment') . ' AS r '.

                'ON r.id_value = g.goods_id AND comment_type = 0 AND r.parent_id = 0 AND r.status = 1 ' .

            'LEFT JOIN ' . $GLOBALS['ecs']->table('bonus_type') . ' AS m ' .

                "ON g.bonus_type_id = m.type_id AND m.send_start_date <= '$time' AND m.send_end_date >= '$time'" .

            " LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".

                    "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".

            "WHERE g.goods_id = '$goods_id' AND g.is_delete = 0 " .

            "GROUP BY g.goods_id";

    $row = $GLOBALS['db']->getRow($sql);



    if ($row !== false)

    {

        /* 用户评论级别取整 */

//         $row['comment_rank']  = ceil($row['comment_rank']) == 0 ? 5 : ceil($row['comment_rank']);

    	$row['comment_rank']  = round($row['comment_rank'],2);

        /* 获得商品的销售价格 */

        $row['market_price']        = price_format($row['market_price']);

        $row['shop_price_formated'] = price_format($row['shop_price']);



        /* 修正促销价格 */

        if ($row['promote_price'] > 0)

        {

            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);

        }

        else

        {

            $promote_price = 0;

        }



        /* 处理商品水印图片 */

        $watermark_img = '';



        if ($promote_price != 0)

        {

            $watermark_img = "watermark_promote";

        }

        elseif ($row['is_new'] != 0)

        {

            $watermark_img = "watermark_new";

        }

        elseif ($row['is_best'] != 0)

        {

            $watermark_img = "watermark_best";

        }

        elseif ($row['is_hot'] != 0)

        {

            $watermark_img = 'watermark_hot';

        }



        if ($watermark_img != '')

        {

            $row['watermark_img'] =  $watermark_img;

        }



        $row['promote_price_org'] =  $promote_price;

	

		$row['promote_price'] = ($promote_price > 0) ? price_format($promote_price) : '';



        /* 修正重量显示 */

        $row['goods_weight']  = (intval($row['goods_weight']) > 0) ?

            $row['goods_weight'] . $GLOBALS['_LANG']['kilogram'] :

            ($row['goods_weight'] * 1000) . $GLOBALS['_LANG']['gram'];



        /* 修正上架时间显示 */

        $row['add_time']      = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);



        /* 促销时间倒计时 */

        $time = gmtime();

        if ($time >= $row['promote_start_date'] && $time <= $row['promote_end_date'])

        {

             $row['gmt_end_time']  = $row['promote_end_date'];

        }

        else

        {

            $row['gmt_end_time'] = 0;

        }



        /* 是否显示商品库存数量 */

        $row['goods_number']  = ($GLOBALS['_CFG']['use_storage'] == 1) ? $row['goods_number'] : '';



        /* 修正积风：转换为可使用多少积风（原来是可以使用多少钱的积风） */

        $row['integral']      = $GLOBALS['_CFG']['integral_scale'] ? round($row['integral'] * 100 / $GLOBALS['_CFG']['integral_scale']) : 0;



        /* 修正优惠券 */

        $row['bonus_money']   = ($row['bonus_money'] == 0) ? 0 : price_format($row['bonus_money'], false);



        /* 修正商品图片 */

        $row['goods_img']   = get_image_path($goods_id, $row['goods_img']);

        $row['goods_thumb'] = get_image_path($goods_id, $row['goods_thumb'], true);

		

		if($promote_price > 0)

		{

			$row['save_price']=$row['market_price']-$promote_price;

		}

		else

		{

		  $row['save_price']=$row['market_price']-$row['shop_price_formated'];  //节省

		}

		

		$row['comment_rank2']  = $row['comment_rank']/5*100;  //评分
		
		
			

        return $row;

    }

    else

    {

        return false;

    }

}


/**
 * 获得指定商品的关联商品
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_linked_goods($goods_id)
{
// 	$sql = 'SELECT g.goods_id, g.goods_name, g.goods_thumb,g.original_img, g.goods_img, g.shop_price  ' .
	$sql = 'SELECT g.goods_id, g.goods_name, g.original_img,  g.shop_price  ' .
			'FROM ' . $GLOBALS['ecs']->table('link_goods') . ' AS lgg ' .
			'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = lgg.link_goods_id ' .
			"WHERE lgg.goods_id = '$goods_id' AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 limit 4 ";
				
			$res = $GLOBALS['db']->query($sql);
// 	return ;
// 	echo "aaa";
	if($res==false)
	{
		return array();
	}
	$arr = array();
	while ($row = $GLOBALS['db']->fetchRow($res))
	{
		$temp = array();
		$temp['goods_id']     = $row['goods_id'];
		$temp['goods_name']   = sub_str($row['goods_name'],10);
		// 		$arr[$row['goods_id']]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
		// 		sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
		$temp['goods_thumb']  = get_image_path($row['goods_id'], $row['goods_thumb'], true);
		$temp['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);
		$temp['market_price'] = price_format($row['market_price']);
		$temp['shop_price']   = price_format($row['shop_price']);
		// 		$arr[$row['goods_id']]['url']          = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);

		// 		if ($row['promote_price'] > 0)
			// 		{
			// 			$arr[$row['goods_id']]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
			// 			$arr[$row['goods_id']]['formated_promote_price'] = price_format($arr[$row['goods_id']]['promote_price']);
			// 		}
			// 		else
				// 		{
				// 			$arr[$row['goods_id']]['promote_price'] = 0;
				// 		}
				
		$arr[] = $temp;
	}

	return $arr;
}

/**

 * 取得订单商品

 * @param   int     $order_id   订单id

 * @return  array   订单商品数组

 */

function order_goods2($order_id)

{

    $sql = "SELECT a.goods_id, a.goods_name, a.goods_sn, a.market_price, a.goods_number, " .

            "a.goods_price, a.goods_attr,  " .

            "a.goods_price * a.goods_number AS subtotal,b.goods_id,b.original_img " .

            "FROM " . $GLOBALS['ecs']->table('order_goods') . " a," . $GLOBALS['ecs']->table('goods') . " b WHERE b.goods_id=a.goods_id and a.order_id = '$order_id'";



    $res = $GLOBALS['db']->getAll($sql);



//     while ($row = $GLOBALS['db']->fetchRow($res))

//     {

//         $goods_list[] = $row;

//     }


	
    //return $GLOBALS['db']->getAll($sql);

    return $res;

}



/**

 * 取得购物车商品

 * @param   int     $type   类型：默认普通商品

 * @return  array   购物车商品数组

 */

function cart_goods_list($type = CART_GENERAL_GOODS)

{

    $sql = "SELECT a.rec_id, a.user_id, a.goods_id, a.goods_name, a.goods_sn, a.goods_number, " .

            "a.market_price, a.goods_price, a.goods_attr, a.is_real, a.extension_code, a.parent_id, a.is_gift, a.is_shipping, " .

            "a.goods_price * a.goods_number AS subtotal,b.goods_id,b.goods_thumb " .

            "FROM " . $GLOBALS['ecs']->table('cart') . "a," . $GLOBALS['ecs']->table('goods') .

            " b WHERE b.goods_id=a.goods_id and a.session_id = '" . SESS_ID . "' " .

            "AND a.rec_type = '$type'";



    $arr = $GLOBALS['db']->getAll($sql);



    /* 格式化价格及礼包商品 */

    foreach ($arr as $key => $value)

    {

        $arr[$key]['formated_market_price'] = price_format($value['market_price'], false);

        $arr[$key]['formated_goods_price']  = price_format($value['goods_price'], false);

        $arr[$key]['formated_subtotal']     = price_format($value['subtotal'], false);



        if ($value['extension_code'] == 'package_buy')

        {

            $arr[$key]['package_goods_list'] = get_package_goods($value['goods_id']);

        }

    }



    return $arr;

}



/**

 * 获得推荐商品

 *

 * @access  public

 * @param   string      $type       推荐类型，可以是 best, new, hot

 * @return  array

 */

function get_recommend_goods_wap($type = '', $num, $cats = '')

{

    if (!in_array($type, array('best', 'new', 'hot')))

    {

        return array();

    }



    //取不同推荐对应的商品

    static $type_goods = array();

    if (empty($type_goods[$type]))

    {

        //初始化数据

        $type_goods['best'] = array();

        $type_goods['new'] = array();

        $type_goods['hot'] = array();

        $data = read_static_cache('recommend_goods');

        if ($data === false)

        {

            $sql = 'SELECT g.goods_id, g.is_best, g.is_new, g.is_hot, g.is_promote, b.brand_name,g.sort_order ' .

               ' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .

               ' LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .

               ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND (g.is_best = 1 OR g.is_new =1 OR g.is_hot = 1)'.

               ' ORDER BY g.sort_order, g.last_update DESC';

            $goods_res = $GLOBALS['db']->getAll($sql);

            //定义推荐,最新，热门，促销商品

            $goods_data['best'] = array();

            $goods_data['new'] = array();

            $goods_data['hot'] = array();

            $goods_data['brand'] = array();

            if (!empty($goods_res))

            {

                foreach($goods_res as $data)

                {

                    if ($data['is_best'] == 1)

                    {

                        $goods_data['best'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);

                    }

                    if ($data['is_new'] == 1)

                    {

                        $goods_data['new'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);

                    }

                    if ($data['is_hot'] == 1)

                    {

                        $goods_data['hot'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);

                    }

                    if ($data['brand_name'] != '')

                    {

                        $goods_data['brand'][$data['goods_id']] = $data['brand_name'];

                    }

                }

            }

            write_static_cache('recommend_goods', $goods_data);

        }

        else

        {

            $goods_data = $data;

        }



        $time = gmtime();

        $order_type = $GLOBALS['_CFG']['recommend_order'];



        //按推荐数量及排序取每一项推荐显示的商品 order_type可以根据后台设定进行各种条件显示

        static $type_array = array();

        $type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot');

        if (empty($type_array))

        {

            foreach($type2lib as $key => $data)

            {

                if (!empty($goods_data[$key]))

                {

                    $data_count = count($goods_data[$key]);

                    $num = $data_count > $num  ? $num : $data_count;

                    if ($order_type == 0)

                    {

                        //usort($goods_data[$key], 'goods_sort');

                        $rand_key = array_slice($goods_data[$key], 0, $num);

                        foreach($rand_key as $key_data)

                        {

                            $type_array[$key][] = $key_data['goods_id'];

                        }

                    }

                    else

                    {

                        $rand_key = array_rand($goods_data[$key], $num);

                        if ($num == 1)

                        {

                            $type_array[$key][] = $goods_data[$key][$rand_key]['goods_id'];

                        }

                        else

                        {

                            foreach($rand_key as $key_data)

                            {

                                $type_array[$key][] = $goods_data[$key][$key_data]['goods_id'];

                            }

                        }

                    }

                }

                else

                {

                    $type_array[$key] = array();

                }

            }

        }



        //取出所有符合条件的商品数据，并将结果存入对应的推荐类型数组中

        $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .

                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".

                "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img, g.original_img, RAND() AS rnd " .

                'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .

                "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".

                "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ";

        $type_merge = array_merge($type_array['new'], $type_array['best'], $type_array['hot']);

        $type_merge = array_unique($type_merge);

        $sql .= ' WHERE g.goods_id ' . db_create_in($type_merge);

        $sql .= ' ORDER BY g.sort_order, g.last_update DESC';



        $result = $GLOBALS['db']->getAll($sql);

        foreach ($result AS $idx => $row)

        {

            if ($row['promote_price'] > 0)

            {

                $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);

                $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';

            }

            else

            {

                $goods[$idx]['promote_price'] = '';

            }



            $goods[$idx]['id']           = $row['goods_id'];

            $goods[$idx]['name']         = $row['goods_name'];

            $goods[$idx]['brief']        = $row['goods_brief'];

            $goods[$idx]['brand_name']   = isset($goods_data['brand'][$row['goods_id']]) ? $goods_data['brand'][$row['goods_id']] : '';

            $goods[$idx]['goods_style_name']   = add_style($row['goods_name'],$row['goods_name_style']);



            $goods[$idx]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?

                                               sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];

            $goods[$idx]['short_style_name']   = add_style($goods[$idx]['short_name'],$row['goods_name_style']);

            $goods[$idx]['market_price'] = price_format($row['market_price']);

            $goods[$idx]['shop_price']   = price_format($row['shop_price']);

            $goods[$idx]['thumb']        = get_image_path($row['goods_id'], $row['goods_thumb'], true);

            $goods[$idx]['original_img']        = get_image_path($row['goods_id'], $row['original_img'], true);

            $goods[$idx]['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);

            $goods[$idx]['url']          = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);

            if (in_array($row['goods_id'], $type_array['best']))

            {

                $type_goods['best'][] = $goods[$idx];

            }

            if (in_array($row['goods_id'], $type_array['new']))

            {

                $type_goods['new'][] = $goods[$idx];

            }

            if (in_array($row['goods_id'], $type_array['hot']))

            {

                $type_goods['hot'][] = $goods[$idx];

            }

        }

    }

    return $type_goods[$type];

}






?>